package com.kaluli.system.security;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;

public class SimpleUsernamePasswordAuthenticationFilter extends
		AbstractAuthenticationProcessingFilter {

	private final static String USER_HEADER = "user";
	private final static String PASS_HEADER = "pass";

	public SimpleUsernamePasswordAuthenticationFilter() {
		super("/*");
	}

	@Override
	public Authentication attemptAuthentication(HttpServletRequest request,
			HttpServletResponse response) throws AuthenticationException,
			IOException, ServletException {
		String user = request.getHeader(USER_HEADER);
		String pass = request.getHeader(PASS_HEADER);
		SimpleUsernamePasswordAuthenticationToken token = new SimpleUsernamePasswordAuthenticationToken(
				user, pass);
		return getAuthenticationManager().authenticate(token);
	}
}
